#include "UICommon.h"

CUICommon::CUICommon(void)
{
}

CUICommon::~CUICommon(void)
{
}

CUIRect CUICommon::GetRect(long ptX1, long ptY1, long ptX2, long ptY2)
{
	long ptStartX = (ptX1 < ptX2) ? ptX1:ptX2;
	long ptStartY = (ptY1 < ptY2) ? ptY1:ptY2;
	long ptEndX = (ptX1 > ptX2) ? ptX1:ptX2;
	long ptEndY = (ptY1 > ptY2) ? ptY1:ptY2;
	return CUIRect(ptStartX,ptStartY,ptEndX - ptStartX,ptEndY - ptStartY);
}

long CUICommon::GetWidth(long ptX1, long ptY1, long ptX2, long ptY2)
{
	return GetRect(ptX1,ptX2,ptY1,ptY2).Width;
}
long CUICommon::GetHeight(long ptX1, long ptY1, long ptX2, long ptY2)
{
	return GetRect(ptX1,ptX2,ptY1,ptY2).Height;
}
bool CUICommon::MakeRoundRect(GraphicsPath * pPath,float PercentageRounded,CUIRect rc)
{
	if(NULL != pPath && rc.Width != 0 && rc.Height != 0)
	{
		REAL left  = (REAL)rc.X;
		REAL right = (REAL)rc.X + (REAL)rc.Width;
		REAL top    = (REAL)rc.Y;
		REAL bottom = (REAL)rc.Y + (REAL)rc.Height;
		REAL offsetX = (right-left) * PercentageRounded / 100;  
		REAL offsetY = (bottom-top) * PercentageRounded / 100;
		pPath->AddArc(right-offsetX, top, offsetX, offsetY, 270, 90);
		pPath->AddArc(right-offsetX, bottom-offsetY, offsetX, offsetY, 0, 90);
		pPath->AddArc(left, bottom - offsetY, offsetX, offsetY, 90, 90);
		pPath->AddArc(left, top, offsetX, offsetY, 180, 90);
		pPath->AddLine(left + offsetX / 2, top, right - offsetX / 2, top);
		return true;
	}
	return false;
}
CUIPoint CUICommon::LayerPtToWindowPt(long ptX, long ptY,CUIStructLayer * pLayer)
{
	return CUIPoint(pLayer->lPtX + ptX,pLayer->lPtY + ptY);
}
CUIPoint CUICommon::WindowPtToLayerPt(long ptX, long ptY,CUIStructLayer * pLayer)
{
	return CUIPoint(ptX - pLayer->lPtX  ,ptY - pLayer->lPtY);
}
bool CUICommon::HitTest(long ptX,long ptY,CUIRect * pUIrc)
{
	Rect rc(pUIrc->X,pUIrc->Y,pUIrc->Width,pUIrc->Height);
	GraphicsPath path;
	path.AddRectangle(rc);	
	if(path.IsVisible(ptX,ptY))
	{	
		return true;
	}
	//Pen mPen(Color::Black, 1);
	//path.Widen(&mPen);
	//if(path.IsVisible(ptY,ptY))
	//{	
	//	return true;		
	//}
	return false;
}